﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using ModelLayer.Reports;

namespace DataAccess
{
    public static class GestorBD
    {

        private static string _connectionString;
        private static SqlConnection _conexion;
        private static SqlCommand _comando;
        private static DataGridView _dataGridView;
        private static string _query;

        private static void Conectar()
        {
            _connectionString = @"Data Source=RISERVER;Initial Catalog=RIS;User ID=sa;Password=MedSoft01";
            _conexion = new SqlConnection(_connectionString);
            _conexion.Open();

        }

        private static void ConectarPACS()
        {
            _connectionString = @"Data Source=RISERVER;Initial Catalog=ImageServer;User ID=sa;Password=MedSoft01";
            _conexion = new SqlConnection(_connectionString);
            _conexion.Open();

        }

        private static void Desconectar()
        {
            _conexion.Close();
        }

        public static DataTable EjecutarSentenciaEnPACS()
        {
            ConectarPACS();
            _comando = new SqlCommand(_query, _conexion);
            SqlDataAdapter adaptador = new SqlDataAdapter(_comando);
            DataTable datos = new DataTable();
            adaptador.Fill(datos);
            //_dataGridView.DataSource = datos;
            _conexion.Close();
            return datos;
        }

        public static DataTable EjecutarSentencia()
        {
            Conectar();
            _comando = new SqlCommand(_query, _conexion);
            SqlDataAdapter adaptador = new SqlDataAdapter(_comando);
            DataTable datos = new DataTable();
            adaptador.Fill(datos);
            //_dataGridView.DataSource = datos;
            _conexion.Close();
            return datos;
        }

        public static void InstanciarAtributos(string query, DataGridView dataGridView)
        {
            _dataGridView = dataGridView;
            _query = query;
        }

        public static string Query
        {
            set
            {
                _query = value;
            }
        }

        public static DataTable InsertNewDictation_SP(Report r)
        {
            Conectar();
            SqlDataAdapter MyDataAdapter = new SqlDataAdapter("InsertNewDictation_SP", _conexion);
            MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

            MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@procedureOID", SqlDbType.UniqueIdentifier));
            MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@type", SqlDbType.Int));
            MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@relation", SqlDbType.Int));
            MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@path", SqlDbType.NText));
            MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@author", SqlDbType.UniqueIdentifier));
            MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@creationTime", SqlDbType.DateTime));


            MyDataAdapter.SelectCommand.Parameters["@procedureOID"].Value = r.ProcedureOID;
            MyDataAdapter.SelectCommand.Parameters["@type"].Value = r.Type;
            MyDataAdapter.SelectCommand.Parameters["@relation"].Value = r.Relation;
            MyDataAdapter.SelectCommand.Parameters["@path"].Value = r.Path;
            MyDataAdapter.SelectCommand.Parameters["@author"].Value = new Guid(r.Author);
            MyDataAdapter.SelectCommand.Parameters["@creationTime"].Value = r.CreationTime;


            DataTable datos = new DataTable();
            MyDataAdapter.Fill(datos);
            MyDataAdapter.Dispose();
            _conexion.Close();
            return datos;
        }

        /*MAURICIO FRIAS: 06/01/2011 09:55am
         * Adicion de ejecucion  de Stored Procedure 

        public static DataTable InsertarNuevoTempPaciente_SP(TempPatient p)
        {
            Conectar();
            SqlDataAdapter MyDataAdapter = new SqlDataAdapter("InsertNewTempPatient_SP", _conexion);
            MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

             MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@mrnId", SqlDbType.NVarChar, 50));
             MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@familyName", SqlDbType.NVarChar, 40));
             MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@givenName", SqlDbType.NVarChar, 40));
             MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@middleName", SqlDbType.NVarChar, 40));
             MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@birthDate", SqlDbType.DateTime));
             MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@sex", SqlDbType.NVarChar, 255));
             MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@telephoneNumber", SqlDbType.NVarChar, 10));

            MyDataAdapter.SelectCommand.Parameters["@mrnId"].Value = p.MrnId;
            MyDataAdapter.SelectCommand.Parameters["@familyName"].Value = p.FamilyName;
            MyDataAdapter.SelectCommand.Parameters["@givenName"].Value = p.GivenName;
            MyDataAdapter.SelectCommand.Parameters["@middleName"].Value = p.MiddleName;
            MyDataAdapter.SelectCommand.Parameters["@birthDate"].Value = p.BirthDate;
            MyDataAdapter.SelectCommand.Parameters["@sex"].Value = p.Sex;
            MyDataAdapter.SelectCommand.Parameters["@telephoneNumber"].Value = p.TelephoneNumber;

            DataTable datos = new DataTable();
            MyDataAdapter.Fill(datos);
            MyDataAdapter.Dispose();
            _conexion.Close();
            return datos;
        }
         < MAURICIO FRIAS 06/01/2011 10:14am */

        public static void Actualizacion()
        {
            Conectar();

            _comando = new SqlCommand(_query, _conexion);

            _comando.ExecuteNonQuery();
            _conexion.Close();
        }


        
    }
}
